package com.zhang;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

/**
 * Inirealm测试
 */
public class IniRealmTest {


    @Test
    public void testAuthentication()
    {
        IniRealm iniRealm=new IniRealm("classpath:user.ini");
//        1.构建SecurityManager环境
        DefaultSecurityManager defaultSecurityManager= new DefaultSecurityManager();
        defaultSecurityManager.setRealm(iniRealm);
//        2.主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject=SecurityUtils.getSubject();
//        3.获取认证数据（从数据库）
        UsernamePasswordToken token =new UsernamePasswordToken("zhang","123");
        System.out.println("是否认证的结果："+subject.isAuthenticated());
        subject.login(token);
        System.out.println("是否认证的结果："+subject.isAuthenticated());
        //-------------------------------判断用户角色，查看用户权限
        subject.checkRole("admin");
        subject.checkPermission("user:delete");
        subject.checkPermission("user:update"); //没有的权限

    }
}
